/* main.vala
 *
 * Copyright (C) 2008  Frederik 'playya' Sdun
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author:
 * 	Frederik 'playya' Sdun <Frederik.Sdun@gmail.com>
 */

using GLib;
using Rob;


public class Robd : Object {
	private GLib.List<Rob.Ruleset> rulesets;
	public Robd () {
		this.rulesets = new GLib.List<Rob.Ruleset>();
	}

		
	public void load()
	{
		string home = GLib.Environment.get_home_dir();
		string confpath = home.concat("/.rob/");
		weak GLib.List filelist = GLib.g_io_modules_load_all_in_directory(confpath);
		for(int i = 0; i < filelist.length(); i++)
		{
			string file = (string)filelist.nth_data(i);
			GLib.KeyFile kf = new GLib.KeyFile();
			kf.load_from_file(file,GLib.KeyFileFlags.KEEP_COMMENTS|GLib.KeyFileFlags.KEEP_TRANSLATIONS);
			Rob.Ruleset rs = new Rob.Ruleset();
			rs.load(kf);
			this.rulesets.prepend(rs);
		}
	}
	public void run()
	{
		for (int i = 0; i < this.rulesets.length() ; i ++)
		{
			Rob.Ruleset rs = this.rulesets.nth_data(i);
			rs.run();
			GLib.debug("Ruleset %s started\n",rs.title);
		}
		Rob.mainloop.run();
		GLib.debug("MainLoop started\n");
	}

	public void stop()
	{
			
		for (int i = 0; i < this.rulesets.length() ; i ++)
		{
			Rob.Ruleset rs = this.rulesets.nth_data(i);
			rs.stop();
			GLib.debug("Ruleset %s stopped\n", rs.title);
		}
		Rob.mainloop.quit();
		GLib.debug("MainLoop stopped\n");
	}

	static int main (string[] args) {
		Rob.init(args);

		var main = new Robd ();
		main.load();
		main.run ();
		
		return 0;
	}
}

}
